// pages/customer/customer.js
const app = getApp();
import userApi from '../../service/userApi.js'
let codeConfig = app.globalData.codeConfig
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadTips: '上拉加载',
    showPupFlag: false,
    startPage: 1,
    pageSize: 15,
    loading: false,
    customerData: {},
    optionTalentId: '',
    optionIsTalent: false,
    upAbled: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    if (options.talentId) {
      that.setData({
        optionTalentId: options.talentId
      })
    }
    if (options.isTalent) {
      that.setData({
        optionIsTalent: options.isTalent
      })
    }
    if (that.data.optionIsTalent == 'true') {
      this.getInitData()
    }
  },
  showPup () {
    this.setData({
      showPupFlag : true
    })
  },
  closePup () {
    this.setData({
      showPupFlag : false
    })
  },
  getInitData () {
    var that = this;
    that.setData({
      loading: true,
      loadTips: '加载中...'
    })
    wx.showLoading({
    	title: '请稍后'
    })
    var params = {
      startPage: that.data.startPage,
      pageSize: that.data.pageSize
    }
    userApi.getCustomerList(params).then(res => {
      wx.hideLoading()
      that.setData({
        loading: false
      })
      wx.stopPullDownRefresh();
      if (res.data.errCode === codeConfig.errCode.SUCCESS_CODE) {
        that.setData({
          customerData: res.data.data
        })
        if (res.data.data.customerList.length < that.data.pageSize) {
          that.setData({
            loadTips: '暂无更多数据',
            upAbled: false
          })
        } else if (res.data.data.customerList.length === 0) {
          that.setData({
            loadTips: '暂无数据'
          })
        } else {
          that.setData({
            loadTips: '上拉加载'
          })
        }
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    }).catch(e => {
      console.log(e)
      that.setData({
        loading: false
      })
      wx.stopPullDownRefresh();
      wx.hideLoading()
    })
  },
  // 分享商品按钮
  onShareGoods(e) {
    var url = e.currentTarget.dataset.url
    var talentId = e.currentTarget.dataset.talentid
    if (talentId) {
      wx.reLaunch({
        url: `${url}?talentId=${talentId}`   //注意switchTab只能跳转到带有tab的页面，不能跳转到不带tab的页面
      })
    } else {
      wx.navigateTo({
        url
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // wx.startPullDownRefresh()
    if (this.data.optionIsTalent == 'true') {
      this.setData({
        startPage: 1,
        pageSize: 15,
        upAbled: true // 是否可上拉加载
      })
      this.getInitData()
    } else {
      // 停止下拉动作
      wx.stopPullDownRefresh();
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (!that.data.upAbled) {
      return false
    }
    if (that.data.loading) {
      return false
    }
    that.setData({
      loading: true,
      loadTips: '加载中...',
      startPage: that.data.startPage + 1
    })
    var params = {
      startPage: that.data.startPage,
      pageSize: that.data.pageSize
    }
    userApi.getCustomerList(params).then(res => {
      that.setData({
        loading: false
      })
      if (res.data.errCode === codeConfig.errCode.SUCCESS_CODE) {
        var arr = that.data.customerData.customerList.concat(res.data.data.customerList)
        that.setData({
          'data.customerList': arr
        })
        if (res.data.data.customerList.length < that.data.pageSize) {
          that.setData({
            loadTips: '暂无更多数据',
            startPage: that.data.startPage - 1,
            upAbled: false
          })
        } else {
          that.setData({
            loadTips: '上拉加载'
          })
        }
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    }).catch(e => {
      console.log(e)
      that.setData({
        loading: false
      })
    })
  }
})